package com.mock.ems.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.mock.base.util.CollectionUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MenuServiceImpl implements MenuService {

	@Override
	public List<Map<String,Object>> getMenuListByUserId(Long userId) {
		
		//获取属于用户的第一层菜单(level为1，type为1)
        String querySQL = " select * from ems_menu " +
                " where type = 1 and level = 1 " +
				" and id in(select menu_id from ems_admin_menu where admin_id = ? ) order by display_order asc ";

        List<Record> menuList0 = Db.find(querySQL,userId);
		if (CollectionUtil.isEmpty(menuList0)) return null;
		
		//查询一级菜单下面的子菜单
		List<Map<String,Object>> returnList = new ArrayList<>();
		for (Record parent : menuList0) {
			returnList.add(recurseMenu(parent));
		}
		
		return returnList;
	}
	
	
	private Map<String,Object> recurseMenu(Record parent0) {
		Long parentId = parent0.getLong("id");
		String sql = "select * from ems_menu where parent_id = ? order by display_order asc";
				//"and id in ("
				//+ "select menu_id from ems_admin_user_menu where admin_user_id = ? )";
		List<Record> childList = Db.find(sql, parentId);
		List<Map<String,Object>> returnList = new ArrayList<>();
		if (CollectionUtil.isEmpty(childList)) {
			Map<String,Object> menuMap = record2Map(parent0);
			menuMap.put("children", returnList);
			return menuMap;
		}else {
			Map<String,Object> menuMap = record2Map(parent0);
			for (Record parent : childList) {
				returnList.add(recurseMenu(parent));
			}
			menuMap.put("children", returnList);
			return menuMap;
		}
	}
	
	private Map<String,Object> record2Map(Record record) {
		
		Map<String,Object> menuMap = new HashMap<>();
		menuMap.put("id", record.get("id"));
		menuMap.put("name", record.get("name"));
		menuMap.put("url", record.get("url"));
		menuMap.put("level", record.get("level"));
		menuMap.put("type", record.get("type"));
		menuMap.put("badge",record.getInt("badge"));
		menuMap.put("micon",record.get("micon"));
		menuMap.put("parentId", record.get("parent_id"));
		return menuMap;
	}

	@Override
	public void deteleMenuByMenuId(Long menuId) {
		//删除菜单表中的数据
		Db.deleteById("ems_menu",menuId);
		
		//删除ems_admin_user_menu表中的数据
		Db.update("delete from ems_role_menu where menu_id = ? ", menuId);
		
	}


	@Override
	public List<Map<String, Object>> getMenuListByRoleId(Long roleId) {
		//获取属于用户的第一层菜单(level为1，type为1)
		String querySQL = " select * from ems_menu " +
				" where id in " +
				" (select menu_id from ems_role_menu where role_id = ? ) " +
				" and type = 1 and level = 1 order by display_order asc ";

		List<Record> menuList0 = Db.find(querySQL, roleId);
		if (CollectionUtil.isEmpty(menuList0)) return null;

		//查询一级菜单下面的子菜单
		List<Map<String, Object>> returnList = new ArrayList<>();
		for (Record parent : menuList0) {
			returnList.add(recurseRoleMenu(parent, roleId));
		}

		return returnList;
	}
	
	private Map<String,Object> recurseRoleMenu(Record parent0,Long roleId) {
		Long parentId = parent0.getLong("id");
		String sql = "select * from ems_menu where id in ("
				+ "select menu_id from ems_role_menu where role_id = ? ) and parent_id = ? order by display_order asc";
		List<Record> childList = Db.find(sql,roleId,parentId);
		List<Map<String,Object>> returnList = new ArrayList<>();
		if (CollectionUtil.isEmpty(childList)) {
			Map<String,Object> menuMap = record2Map(parent0);
			menuMap.put("children", returnList);
			return menuMap;
		}else {
			Map<String,Object> menuMap = record2Map(parent0);
			for (Record parent : childList) {
				returnList.add(recurseRoleMenu(parent,roleId));
			}
			menuMap.put("children", returnList);
			return menuMap;
		}
	}
}
